package org.practice.model;

import java.util.Set;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.annotations.Where;
/**
 * @author jack
 * 组件映射
 * 组件(component)表示嵌入式对象(@Embedded)，是实体类的逻辑组成部分，它与实体的根本区别是没有oid，它实现了对象模型的细粒度划分
 * 例如：
 * 1,在实体类(Order)关联属性上使 @Embedded 和 @AttributeOverride 注解可以覆盖该属性对应的嵌入式对象(Address)的字段映射
 * @Embedded
 * @AttributeOverrides({
 *   @AttributeOverride(name="receiveName", column=@Column(name="receive_name")),
 *   @AttributeOverride(name="postalCode", column=@Column(name="postal_code"))
 * })
 * 2,在组件类上使用@Emabbedable
 */
@Entity
@Table(name="shop_order")
public class Order implements java.io.Serializable{
	private static final long serialVersionUID=1234567L;
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)//指定生成器的策略
	private Integer id;
	private Boolean status=Boolean.TRUE;// 记录状态 0 无效，1 有效 
	@Column(name="order_time")
	private long orderTime;
	@Column(name="description")
	private String desc;
	@Column(name="order_number")
	private String orderNumber;
	@Column(name="total_price")
	private double totalPrice;
	@Column(name="order_price")
	private double orderPrice;
	@Column(name="send_free")
	private double sendFee;
	
	@OneToMany(mappedBy="order",cascade=CascadeType.ALL)
	@Where(clause="status=1")
	@LazyCollection(LazyCollectionOption.TRUE) 
	private Set<Item> items;//Set集合是无序的，不可重复的集合。因此set元素无须使用index来指定集合元素的次序

	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="user_id",nullable=false)
	private User user;
	
	@Embedded
	@AttributeOverrides({
		@AttributeOverride(name="receiveName", column=@Column(name="receive_name")),
		@AttributeOverride(name="fullAddress", column=@Column(name="full_address")),
		@AttributeOverride(name="postalCode", column=@Column(name="postal_code"))
	})
	private Address address;
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	
	public long getOrderTime() {
		return orderTime;
	}
	public void setOrderTime(long orderTime) {
		this.orderTime = orderTime;
	}
	public String getDesc() {
		return desc;
	}
	public void setDesc(String desc) {
		this.desc = desc;
	}
	public double getTotalPrice() {
		return totalPrice;
	}
	public void setTotalPrice(double totalPrice) {
		this.totalPrice = totalPrice;
	}
	public double getOrderPrice() {
		return orderPrice;
	}
	public void setOrderPrice(double orderPrice) {
		this.orderPrice = orderPrice;
	}
	public double getSendFee() {
		return sendFee;
	}
	public void setSendFee(double sendFee) {
		this.sendFee = sendFee;
	}
	public Set<Item> getItems() {
		return items;
	}
	public void setItems(Set<Item> items) {
		this.items = items;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public String getOrderNumber() {
		return orderNumber;
	}
	public void setOrderNumber(String orderNumber) {
		this.orderNumber = orderNumber;
	}
	public Boolean getStatus() {
		return status;
	}
	public void setStatus(Boolean status) {
		this.status = status;
	}
	
}
